<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>103-列表和元组</title>

<script src="/lib/require/index.js"></script>
<script src="/lib/jquery/index.js"></script>

<!-- 改为引入外部样式 -->
<link rel="stylesheet" href="/css/custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="/lib/mathjax/index.js"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
<div class="toc">
<ul>
                        
                        
                        <li class="toc-li-2">
                            <a href="#列表">列表</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#基本操作">基本操作</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#索引和切片">索引和切片</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#更新列表">更新列表</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#删除列表元素">删除列表元素</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#常用的列表函数">常用的列表函数</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.append(obj)">list.append(obj)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.extend(seq)">list.extend(seq)</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.insert(index,obj)">list.insert(index,obj)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.copy()">list.copy()</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.clear()">list.clear()</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.count(obj)">list.count(obj)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.index(obj)">list.index(obj)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.pop([index])">list.pop([index])</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.remove(obj)">list.remove(obj)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.reverse()">list.reverse()</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#list.sort(key=None,-reverse=False)">list.sort(key=None, reverse=False)</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-2">
                            <a href="#元组">元组</a>
                        </li>
                    
                
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#索引和切片">索引和切片</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#元组用在哪里？">元组用在哪里？</a>
                        </li>
                    
                
            
            
            
            
            
            
            </ul>
</div>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[57]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 打印所有单行变量</span>
<span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="k">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s1">&#39;all&#39;</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#21015;&#34920;">&#21015;&#34920;<a class="anchor-link" href="#&#21015;&#34920;">&#182;</a></h2><p>列表也是一种序列，每个元素以逗号<code>,</code>分开。列表无所不包，里面可以放各种类型的数据，且里面所能容纳的元素数量无限，<br>
当然这是在硬件设备理想的情况下。<code>列表元素可以修改</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#22522;&#26412;&#25805;&#20316;">&#22522;&#26412;&#25805;&#20316;<a class="anchor-link" href="#&#22522;&#26412;&#25805;&#20316;">&#182;</a></h3><p>和前面<a href="./102.html">《字符串》</a>的基本操作类似，在list中，也有类似的操作。只不过是以<strong>元素为单位</strong>，不是以字符为单位进行索引了。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table>
<thead><tr>
<th style="text-align:center"></th>
<th style="text-align:center">实例</th>
<th style="text-align:center">结果</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">len()</td>
<td style="text-align:center">len['ab', 1]</td>
<td style="text-align:center">2</td>
<td style="text-align:center">求序列长度</td>
</tr>
<tr>
<td style="text-align:center"><code>+</code></td>
<td style="text-align:center">['ab']+[1]</td>
<td style="text-align:center">['ab', 1]</td>
<td style="text-align:center">连接2个序列，必须是同类型数据</td>
</tr>
<tr>
<td style="text-align:center"><code>*</code></td>
<td style="text-align:center">['a']*2</td>
<td style="text-align:center">['a', 'a']</td>
<td style="text-align:center">重复序列元素</td>
</tr>
<tr>
<td style="text-align:center">in</td>
<td style="text-align:center">'ab' in ['ab',1]<br>'a' in ['ab',1]</td>
<td style="text-align:center">True<br>False</td>
<td style="text-align:center">判断元素是否存在于序列中</td>
</tr>
<tr>
<td style="text-align:center">max()</td>
<td style="text-align:center">max(['a', 'b'])</td>
<td style="text-align:center">'a'</td>
<td style="text-align:center">返回最大值，大小排序符合字符编码，列表内元素必须是同种类型的数据</td>
</tr>
<tr>
<td style="text-align:center">min()</td>
<td style="text-align:center">min([2, 1])</td>
<td style="text-align:center">1</td>
<td style="text-align:center">返回最小值，大小排序符合字符编码，列表内元素必须是同种类型的数据</td>
</tr>
</tbody>
</table>
<blockquote><p>用比较运算符比较大小，不过必须是同种类型的数据才能比较</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#32034;&#24341;&#21644;&#20999;&#29255;">&#32034;&#24341;&#21644;&#20999;&#29255;<a class="anchor-link" href="#&#32034;&#24341;&#21644;&#20999;&#29255;">&#182;</a></h3><p>默认从0开始计数</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[58]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">]]</span>

<span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>  <span class="c1"># --&gt;</span>
<span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>  <span class="c1"># --&gt;</span>
<span class="n">y</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">)</span>  <span class="c1"># --&gt; 检索该元素在列表中第一次出现的位置</span>
<span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[58]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;a&#39;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[58]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>1</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[58]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>0</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[58]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;a&#39;</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table>
<thead><tr>
<th style="text-align:center">切片</th>
<th style="text-align:center">结果</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">list[start:end:step]</td>
<td style="text-align:center">默认正向切片</td>
<td style="text-align:center">切片取值：前包括后不包括<br>end必须大于start,否则返回空<br>step为空时，缺省值为1<br>step&lt;0时，则反向切片</td>
</tr>
<tr>
<td style="text-align:center">y[1:]</td>
<td style="text-align:center">[ 1, 'a', [3]]</td>
<td style="text-align:center">得到从2号到最末尾的元素,这时最后那个可以不写</td>
</tr>
<tr>
<td style="text-align:center">y[:]</td>
<td style="text-align:center">['a', 1, 'a', [3]]</td>
<td style="text-align:center">得到所有元素</td>
</tr>
<tr>
<td style="text-align:center">y[:2]</td>
<td style="text-align:center">['a', 1]</td>
<td style="text-align:center">得到2之前(不包括第三个)的元素</td>
</tr>
<tr>
<td style="text-align:center">y[0:3:2]</td>
<td style="text-align:center">['a', 'a']</td>
<td style="text-align:center">得到以step=2从0到3(不包括3)的元素</td>
</tr>
<tr>
<td style="text-align:center">y[::-1]</td>
<td style="text-align:center">[[3], 'a', 1, 'a']</td>
<td style="text-align:center">反向得到所有元素</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>如果列表内有序列(字符串、列表等)，则可以进行二次索引和切片</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[59]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;abc&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">]]</span>

<span class="n">y</span><span class="p">[</span><span class="mi">2</span><span class="p">][:</span><span class="mi">2</span><span class="p">]</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[59]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;ab&#39;</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#26356;&#26032;&#21015;&#34920;">&#26356;&#26032;&#21015;&#34920;<a class="anchor-link" href="#&#26356;&#26032;&#21015;&#34920;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[60]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;abc&#39;</span><span class="p">]</span>
<span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;python&#39;</span>

<span class="n">y</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[60]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;a&#39;, &#39;python&#39;, &#39;abc&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#21024;&#38500;&#21015;&#34920;&#20803;&#32032;">&#21024;&#38500;&#21015;&#34920;&#20803;&#32032;<a class="anchor-link" href="#&#21024;&#38500;&#21015;&#34920;&#20803;&#32032;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[61]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;abc&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">]</span>
<span class="k">del</span> <span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

<span class="n">y</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[61]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;a&#39;, &#39;abc&#39;, &#39;a&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#24120;&#29992;&#30340;&#21015;&#34920;&#20989;&#25968;">&#24120;&#29992;&#30340;&#21015;&#34920;&#20989;&#25968;<a class="anchor-link" href="#&#24120;&#29992;&#30340;&#21015;&#34920;&#20989;&#25968;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[62]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">([])</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)]</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[62]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;append&#39;,
 &#39;clear&#39;,
 &#39;copy&#39;,
 &#39;count&#39;,
 &#39;extend&#39;,
 &#39;index&#39;,
 &#39;insert&#39;,
 &#39;pop&#39;,
 &#39;remove&#39;,
 &#39;reverse&#39;,
 &#39;sort&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[63]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">help</span><span class="p">([]</span><span class="o">.</span><span class="n">append</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>Help on built-in function append:

append(...) method of builtins.list instance
    L.append(object) -&gt; None -- append object to end

</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.append(obj)">list.append(obj)<a class="anchor-link" href="#list.append(obj)">&#182;</a></h4><p>在列表末尾添加新的<code>对象</code>，修改原对象且<code>没有返回值</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[64]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;python&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>  <span class="c1"># 修改的是原始列表，没有返回值</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>None
[&#39;python&#39;, 1, 2]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.extend(seq)">list.extend(seq)<a class="anchor-link" href="#list.extend(seq)">&#182;</a></h4><p>在列表末尾一次性追加<code>另一个序列</code>中的多个值，修改原对象且<code>没有返回值</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[65]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;python&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">a</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>

<span class="n">a</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[65]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;python&#39;, 1, 1, 2]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>当追加的是字符串时，字符串会以字符为单位拆开</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[66]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="s1">&#39;ab&#39;</span><span class="p">)</span>
<span class="n">a</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[66]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;python&#39;, 1, 1, 2, &#39;a&#39;, &#39;b&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.insert(index,obj)">list.insert(index,obj)<a class="anchor-link" href="#list.insert(index,obj)">&#182;</a></h4><p>将对象<code>obj</code>插入到列表中<code>index</code>位置，原位置向后移动。修改原对象且<code>没有返回值</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[67]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;python&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">a</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">)</span>

<span class="n">a</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[67]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;python&#39;, &#39;a&#39;, 1]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.copy()">list.copy()<a class="anchor-link" href="#list.copy()">&#182;</a></h4><p><code>返回</code>复制后的新列表，类似a[:]</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.clear()">list.clear()<a class="anchor-link" href="#list.clear()">&#182;</a></h4><p>清空列表，类似del a[:]，修改原对象且<code>没有返回值</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.count(obj)">list.count(obj)<a class="anchor-link" href="#list.count(obj)">&#182;</a></h4><p><code>返回</code>某个元素在列表中出现的次数，不存在则返回0，不会报错</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[68]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">a</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
<span class="n">a</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[68]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>3</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[68]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>0</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.index(obj)">list.index(obj)<a class="anchor-link" href="#list.index(obj)">&#182;</a></h4><p>检索该元素在列表中第一次出现的位置。<code>返回</code>查找对象的索引位置，如果没有找到对象则抛出异常</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.pop([index])">list.pop([index])<a class="anchor-link" href="#list.pop([index])">&#182;</a></h4><p>移除列表中的一个元素（默认最后一个元素），修改原对象并且<code>返回</code>该元素的值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[69]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;python&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>

<span class="n">a</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>   <span class="c1"># --&gt;</span>
<span class="n">a</span>         <span class="c1"># --&gt;</span>
<span class="n">a</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
<span class="n">a</span>         <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[69]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>2</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[69]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;python&#39;, 1, 1]</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[69]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;python&#39;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[69]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[1, 1]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.remove(obj)">list.remove(obj)<a class="anchor-link" href="#list.remove(obj)">&#182;</a></h4><p>移除列表中某个值的<code>第一个</code>匹配项。修改原对象且<code>没有返回值</code>，如果所删除的内容不在列表中，就报错。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.reverse()">list.reverse()<a class="anchor-link" href="#list.reverse()">&#182;</a></h4><p>将列表的元素顺序反过来。修改原对象且<code>没有返回值</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[70]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">6</span><span class="p">]</span>
<span class="n">a</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>

<span class="n">a</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[70]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[6, 1, 5, 3]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="list.sort(key=None,-reverse=False)">list.sort(key=None, reverse=False)<a class="anchor-link" href="#list.sort(key=None,-reverse=False)">&#182;</a></h4><p>key接受一个函数，这个函数只接受一个元素，默认为None<br>
reverse是一个布尔值。如果设置为True，列表元素将被倒序排列，默认为False<br>
修改原对象且<code>没有返回值</code>。默认情况，实现的是从小到大的排序。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[71]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">6</span><span class="p">]</span>
<span class="n">a</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<span class="n">a</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[71]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[6, 5, 3, 1]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>python3内置函数sorted(iterable，key=None,reverse=False)可<code>返回</code>一个列表</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[75]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">students</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;john&#39;</span><span class="p">,</span> <span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="mi">15</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;jane&#39;</span><span class="p">,</span> <span class="s1">&#39;B&#39;</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;dave&#39;</span><span class="p">,</span><span class="s1">&#39;B&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)]</span>
<span class="nb">sorted</span><span class="p">(</span><span class="n">students</span><span class="p">,</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">students</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="c1"># 按照年龄来排序</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[75]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[(&#39;john&#39;, &#39;A&#39;, 15), (&#39;jane&#39;, &#39;B&#39;, 12), (&#39;dave&#39;, &#39;B&#39;, 10)]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#20803;&#32452;">&#20803;&#32452;<a class="anchor-link" href="#&#20803;&#32452;">&#182;</a></h2><p>元组使用小括号<code>(</code> <code>)</code>每个元素以逗号<code>,</code>分开。元组与列表类似，不同之处在于元组的元素不能修改</p>
<p>注意：元组中只包含一个元素时，需要在元素后面添加逗号</p>
<h3 id="&#32034;&#24341;&#21644;&#20999;&#29255;">&#32034;&#24341;&#21644;&#20999;&#29255;<a class="anchor-link" href="#&#32034;&#24341;&#21644;&#20999;&#29255;">&#182;</a></h3><p>元组的基本操作就和列表类似。</p>
<h3 id="&#20803;&#32452;&#29992;&#22312;&#21738;&#37324;&#65311;">&#20803;&#32452;&#29992;&#22312;&#21738;&#37324;&#65311;<a class="anchor-link" href="#&#20803;&#32452;&#29992;&#22312;&#21738;&#37324;&#65311;">&#182;</a></h3><p>元组有这类特点，并且是它使用的情景:</p>
<ul>
<li>元组比列表操作速度快。如果您定义了一个值的常量集，并且唯一要用它做的是不断地遍历它，请使用元组代替列表。</li>
<li>如果对不需要修改的数据进行 “写保护”，可以使代码更安全</li>
<li>元组可以在字典中被用做 key，但是列表不行</li>
<li>元组可以用在字符串格式化中。</li>
</ul>

</div>
</div>
</div>
    </div>
  </div>
</body>

 


</html>
